package cn.edu.ecnu.gis.client;

import cn.edu.ecnu.gis.client.alert.view.MessengerView;
import cn.edu.ecnu.gis.client.gadgets.CalendarGadget;
import cn.edu.ecnu.gis.client.gadgets.DataListGadget;
import cn.edu.ecnu.gis.client.gadgets.DiceWarsGadget;
import cn.edu.ecnu.gis.client.gadgets.FiveOneMapsGadget;
import cn.edu.ecnu.gis.client.gadgets.GoogleMapsGadget;
import cn.edu.ecnu.gis.client.gadgets.HelloWorldGadget;
import cn.edu.ecnu.gis.client.gadgets.LTSearchGadget;
import cn.edu.ecnu.gis.client.gadgets.LanlonGadget;
import cn.edu.ecnu.gis.client.gadgets.MailListGadget;
import cn.edu.ecnu.gis.client.gadgets.MapEditGadget;
import cn.edu.ecnu.gis.client.gadgets.NewsGadget;
import cn.edu.ecnu.gis.client.gadgets.RouteLayer;
import cn.edu.ecnu.gis.client.gadgets.ToDoListGadget;
import cn.edu.ecnu.gis.client.liwamap.SVGMapGadget;

import cn.edu.ecnu.gis.client.model.GadgetClass;
import cn.edu.ecnu.gis.client.view.DesktopView;
import cn.edu.ecnu.gis.client.view.DesktopViewListener;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ImageBundle;
import com.google.gwt.user.client.ui.RootPanel;

import com.google.gwt.user.client.ui.VerticalPanel;
import java.util.HashMap;

public class MashupGISEntryPoint implements EntryPoint, DesktopViewListener {

    Storage storage;
    DesktopView view;
    private VerticalPanel mainPanel = new VerticalPanel();
    private HorizontalPanel topPanel = new HorizontalPanel();
    public static HashMap widgetsList = new HashMap();

    public static interface BrandImageBundle extends ImageBundle {

        public AbstractImagePrototype brandIcon();
    }
    public static BrandImageBundle brandImageBundle = (BrandImageBundle) GWT.create(BrandImageBundle.class);
    private Image brandIcon = brandImageBundle.brandIcon().createImage();

    public void onModuleLoad() {
        //add gadgets
        GadgetClass.addClass(new HelloWorldGadget.Class());
        GadgetClass.addClass(new CalendarGadget.Class());
        GadgetClass.addClass(new ToDoListGadget.Class());
        GadgetClass.addClass(new GoogleMapsGadget.Class());
        GadgetClass.addClass(new DataListGadget.Class());
        GadgetClass.addClass(new MailListGadget.Class());
        GadgetClass.addClass(new NewsGadget.Class());
        GadgetClass.addClass(new DiceWarsGadget.Class());
        GadgetClass.addClass(new LanlonGadget.Class());
        GadgetClass.addClass(new MessengerView.Class());
        GadgetClass.addClass(new FiveOneMapsGadget.Class());
        GadgetClass.addClass(new LTSearchGadget.Class());
        //GadgetClass.addClass(new GoogleMapsGadget2.Class());
        GadgetClass.addClass(new LTSearchGadget.Class());
        GadgetClass.addClass(new SVGMapGadget.Class());
        GadgetClass.addClass(new RouteLayer.Class());
        GadgetClass.addClass(new MapEditGadget.Class());
//        Object gadget;
//        try {
//            gadget = Class.forName("cn.edu.ecnu.gis.client.gadgets.MapEditGadget.Class");
//            GadgetClass.addClass((GadgetClass) gadget);
//        } catch (ClassNotFoundException ex) {
//           ex.toString();
//        }


        //create view
        view = new DesktopView(this);
        topPanel.setStyleName("mashupgis");
        HTML title = new HTML("Mash@GIS");
        title.setStyleName("titleStyle");

        topPanel.add(title);
        //topPanel.add(brandIcon);
        mainPanel.add(topPanel);
        mainPanel.setWidth("100%");
        mainPanel.setHeight("100%");
        mainPanel.add(view);
        //mainPanel.setVerticalAlignment(VerticalPanel.ALIGN_TOP);
        RootPanel.get("DesktopView").add(mainPanel);

        //load layout from storage
        if (GearsStorage.isGearsInstalled()) {
            storage = new GearsStorage();
        } else {
            storage = new CookieStorage();
        }
        String layout = storage.getValue("layout");
        if (layout != null) {
            view.setLayoutFromString(layout);
        }
    }

    public void onInterfaceChange() {
        //save layout to storage
        String layout = view.getLayoutAsString();
        storage.setValue("layout", layout);
        try {
            storage.save();
        } catch (StorageException e) {
            Window.alert("unable to save the desktops state");
        }
    }
}
